/****************************************************************************
**
** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef SERIALPORTREADER_H
#define SERIALPORTREADER_H

#include <QtSerialPort/QSerialPort>

#include <QTextStream>
#include <QTimer>
#include <QByteArray>
#include <QObject>
#include "utils.h"

QT_USE_NAMESPACE

QT_BEGIN_NAMESPACE

QT_END_NAMESPACE


namespace PCSG{

class SerialPortReader : public QObject
{
    Q_OBJECT

public:
    // SerialPortReader(QSerialPort *serialPort, QObject *parent = 0);
    SerialPortReader(QObject *parent = 0);
    ~SerialPortReader();


    struct SerialPortSettings {
        QString name;
        qint32 baudRate;
        QSerialPort::DataBits dataBits;
        QSerialPort::Parity parity;
        QSerialPort::StopBits stopBits;
        QSerialPort::FlowControl flowControl;

        SerialPortSettings()
        {
            name = "COM6";
            baudRate = QSerialPort::Baud9600;
            dataBits = QSerialPort::Data8;
            parity = QSerialPort::NoParity;
            stopBits = QSerialPort::OneStop;
            flowControl = QSerialPort::NoFlowControl;
        }
    };

    QString folder_base;
    QString folder_settings;
    QString folder_log;
    QString folder_data;

    QString file_settings;// fixed
    QString file_data;// auto-gened
    QString file_log;// auto-gened

    bool init(
            const QString& folder_base,
            const QString& folder_settings_rel,
            const QString& folder_data_rel,
            const QString& folder_log_rel
            );
    bool open();
    void close();
    bool write(const QByteArray &writeData);
    SerialPortSettings settings;



private slots:
    void SLOT_handleReadyRead();
    void SLOT_handleError(QSerialPort::SerialPortError error);
    void SLOT_handleTimeout_sender();
    void SLOT_handleTimeout_checker();
    //void SLOT_handleBytesWritten(qint64 bytes);

private:
    QSerialPort *m_serialPort;
    QByteArray  m_readData;

    QTimer m_timer_sender;
    QTimer m_timer_checker;

    quint64 cnt_recv;
    int64_t time_interval_check;
    int64_t time_interval_send;
    int64_t time_interval_save;
    int64_t time_out_recv;
    int64_t time_last_save;
    int64_t time_last_recv;

    FILE* p_file_data;
};

}// namespace PCSG
#endif
